<template>
	<view>
		<view class="top">
			<view class="sel-address">
				<view class="sel-address-left">
					<view class="flex align-center" @click="getLocation">
						<image src="/static/images/dzhi1.png"></image>
						当前定位
					</view>
					<view class="address-content">
						{{address||'当前位置'}}
					</view>
				</view>
				<view @click="selLocation" class="reset-location">重新选择</view>
			</view>

			<view class="tips">5km以内的自提点</view>
		</view>

		<view class="list">
			<view class="list-item" v-for="(v,index) in list" :key="index">
				<image :src="$func.showImg(v.img)"></image>
				<view>
					<view class="item-title">
						{{v.name}}
						<!-- <text>0.5km</text> -->
					</view>
					<view class="item-address">
						<!-- 店铺地址：哈尔滨市松北区松浦大道观 江国际A区4号楼1单元102室 -->
						店铺地址{{v.address}}
					</view>
					<view class="item-distance">距您地址：{{v.gap_km}}km</view>

					<view class="sel" @click="selThis(v)">选这个</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Location from '@/utils/location.js'
	export default {
		data() {
			return {
				list: [],
				goodsList: [],

				lng: '',
				lat: '',
				address: '',
			}
		},
		onLoad(option) {
			this.goodsList = JSON.parse(option.lists);
			console.log(this.goodsList, '9889898989');
		},
		mounted() {
			this.getLocation()
			// this.lnglat2address()
			// this.init()
		},
		methods: {
			selLocation() {
				uni.chooseLocation({
					success: res => {
						console.log('321321', res)
						this.address = res.address
						this.lng = res.longitude
						this.lat = res.latitude
						
						this.init()
					}
				})
			},
			async getLocation() {
				const url = location.href.split('#')[0];
				Location.getJsApiConfig(url, res => {
					this.lng = res.longitude
					this.lat = res.latitude
					

					// console.log('-=--=lng-lat', this.lng, this.lat)
					
					this.lnglat2address()
					this.init()
				})
			},
			lnglat2address() {
				uni.request({
					url: '/wanlshop/address/getAddressByLatLng',
					data: {
						longitude: this.lng,
						latitude: this.lat
					},
					success: res => {
						this.address = res.data.regeocode.formatted_address
					}
				})
			},
			init() {
				this.page = 1
				this.getList()
			},
			getList() {
				// if (this.page > this.totalPage) return
				// 提取所有的产品 ID 并拼接成一个字符串
				const allProductIds = this.goodsList
					.flatMap(item => item.products.map(product => product.id)) // 提取每个 products 数组中的 id
					.join(','); // 将所有 id 拼接成逗号分隔的字符串
				uni.request({
					url: '/wanlshop/product/spotList',
					data: {
						good_id: allProductIds,
						page: this.page,
						pointLat: this.lat,
						pointLng: this.lng
					},
					success: res => {
						// console.log('--==--=res', res)
						this.list = res.data
					},
					complete() {
						uni.stopPullDownRefresh()
					}
				})
			},
			selThis(item) {
				console.log('-==-item', item)
				this.$func.setPrevPage('addressData', {
					address: item.address,
					name: item.name,
					mobile: item.phone,
					province: '',
					city: '',
					district: '',
					id: item.id
				})
				uni.navigateBack()
			}
		}
	}
</script>

<style lang="less" scoped>
	.content {
		padding: 25rpx;
		background-color: #f4f4f4;
	}

	.top {
		position: sticky;
		top: 0;

		.sel-address {
			display: flex;
			justify-content: space-between;
			align-items: center;
			gap: 26rpx;
			height: 90rpx;
			padding: 0 23rpx;
			font-weight: 400;
			font-size: 26rpx;
			color: #666666;
			line-height: 42rpx;
			background-color: #fff;

			&-left {
				display: flex;
				align-items: center;
				justify-content: space-between;
				flex: 1;

				image {
					width: 30rpx;
					height: 30rpx;
					margin-right: 10rpx;
				}

				.address-content {
					max-width: 300rpx;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}

			.reset-location {
				font-weight: 400;
				font-size: 26rpx;
				color: #2C4076;
				line-height: 42rpx;
			}
		}

		.tips {
			font-weight: 400;
			font-size: 24rpx;
			color: #FFFFFF;
			line-height: 42rpx;
			height: 50rpx;
			line-height: 50rpx;
			text-align: center;
			background-color: #000;
		}
	}

	.list {
		display: flex;
		flex-direction: column;
		gap: 20rpx;

		.list-item {
			display: flex;
			gap: 23rpx;
			background-color: #fff;
			border-radius: 20rpx;
			padding: 25rpx 17rpx;
			position: relative;

			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}

			>view {
				display: flex;
				flex-direction: column;
				gap: 10rpx;
				flex: 1;

			}

			.item-title {
				font-weight: 400;
				font-size: 30rpx;
				color: #333333;
				line-height: 42rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				text {
					font-weight: 400;
					font-size: 24rpx;
					color: #888888;
					line-height: 36rpx;
				}
			}

			.item-address {
				font-weight: 400;
				font-size: 24rpx;
				color: #666666;
				line-height: 36rpx;
			}
			
			.item-distance{
				font-weight: 400;
				font-size: 24rpx;
				color: #E60013;
				line-height: 36rpx;
			}

			.sel {
				width: 150rpx;
				height: 62rpx;
				line-height: 62rpx;
				text-align: center;
				border-radius: 31rpx;
				background-color: #000;
				border-radius: 100rpx;
				font-family: Source Han Sans CN;
				font-weight: 400;
				font-size: 26rpx;
				color: #FFFFFF;
				position: absolute;
				bottom: 27rpx;
				right: 18rpx;
			}
		}
	}
</style>